package com.demo;

import com.demo.common.JedisConfig;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

/**
 * @author lc
 * @date 2020/12/3
 * @description 自动配置
 */
@Configuration
@EnableConfigurationProperties(Yaml.class)
public class AutoConfig {

    private final Yaml yaml;
    public AutoConfig(Yaml yaml) {
        this.yaml = yaml;
    }

    /**
     * @author lc
     * @date 2020/12/3
     * @description 构建jedis集群
     */
    @Bean
    public JedisCluster jedisCluster() {
        if (yaml.getNodes() == null)
            return null;
        return JedisConfig.getJedisCluster(yaml);
    }

    /**
     * @author lc
     * @date 2020/12/3
     * @description 构建jedis单机
     */
    @Bean
    public Jedis jedis(){
        if (yaml.getHost() == null)
            return null;
        return JedisConfig.getJedis(yaml);
    }

}
